package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.SysGoodsEarn;
import com.ruoyi.system.mapper.SysGoodsEarnMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysGoodsMapper;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.service.ISysGoodsService;

/**
 * 商品库存Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-23
 */
@Service
public class SysGoodsServiceImpl implements ISysGoodsService
{
    @Autowired
    private SysGoodsMapper sysGoodsMapper;
    @Autowired
    private SysGoodsEarnMapper sysGoodsEarnMapper;

    /**
     * 查询商品库存
     *
     * @param goodsId 商品库存主键
     * @return 商品库存
     */
    @Override
    public SysGoods selectSysGoodsByGoodsId(Long goodsId)
    {
        return sysGoodsMapper.selectSysGoodsByGoodsId(goodsId);
    }

    /**
     * 查询商品库存列表
     *
     * @param sysGoods 商品库存
     * @return 商品库存
     */
    @Override
    public List<SysGoods> selectSysGoodsList(SysGoods sysGoods)
    {
        String username = SecurityUtils.getLoginUser().getUsername();
        sysGoods.setUser(username);
        return sysGoodsMapper.selectSysGoodsList(sysGoods);
    }

    /**
     * 新增商品库存
     *
     * @param sysGoods 商品库存
     * @return 结果
     */
    @Override
    public int insertSysGoods(SysGoods sysGoods)
    {
        String username = SecurityUtils.getLoginUser().getUsername();
        sysGoods.setUser(username);
        return sysGoodsMapper.insertSysGoods(sysGoods);
    }

    /**
     * 修改商品库存
     *
     * @param sysGoods 商品库存
     * @return 结果
     */
    @Override
    public int updateSysGoods(SysGoods sysGoods)
    {
        return sysGoodsMapper.updateSysGoods(sysGoods);
    }

    /**
     * 批量删除商品库存
     *
     * @param goodsIds 需要删除的商品库存主键
     * @return 结果
     */
    @Override
    public int deleteSysGoodsByGoodsIds(Long[] goodsIds)
    {
        return sysGoodsMapper.deleteSysGoodsByGoodsIds(goodsIds);
    }

    /**
     * 删除商品库存信息
     *
     * @param goodsId 商品库存主键
     * @return 结果
     */
    @Override
    public int deleteSysGoodsByGoodsId(Long goodsId)
    {
        return sysGoodsMapper.deleteSysGoodsByGoodsId(goodsId);
    }

    /**
     * 修改用户状态
     *
     * @param goods 用户信息
     * @return 结果
     */
    @Override
    public int updateUserStatus(SysGoods goods) {
        SysGoodsEarn sysGoodsEarn = new SysGoodsEarn();
        Integer inventory = sysGoodsMapper.selectSysGoodsByGoodsId(goods.getGoodsId()).getNum();
        Integer num = goods.getNum();
        int newInventory = inventory - num;
        goods.setNum(newInventory);
        if (newInventory>0){
            goods.setStatus("0");
        }else if (newInventory<0){
            return 0;
        }
        BeanUtils.copyBeanProp(sysGoodsEarn,goods);
        sysGoodsEarn.setNum(num);
        //在仓天数
        long time = goods.getDate().getTime();
        long now = new Date().getTime();
        String s = String.valueOf((now - time) / (1000 * 60 * 60 * 24)+1);
        //利润
        BigDecimal price = goods.getSellPrice().subtract(goods.getPrice());
        BigDecimal income = price.multiply(BigDecimal.valueOf(num));
        sysGoodsEarn.setRepositoryTime(s);
        sysGoodsEarn.setIncome(income);
        sysGoodsEarnMapper.insertSysGoodsEarn(sysGoodsEarn);
        return sysGoodsMapper.updateSysGoods(goods);
    }
}
